package edu.upb.acs.jobsmaps.activity.fileuploader;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import edu.upb.acs.jobsmaps.R;
import edu.upb.acs.jobsmaps.fileuploader.FileArrayAdapter;
import edu.upb.acs.jobsmaps.fileuploader.Item;

import java.io.*;
import java.sql.Date;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FileChooserActivity extends ListActivity {

	private File currentDir;
	private FileArrayAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		currentDir = new File("/sdcard/");
		fill(currentDir);
	}

	private void fill(File f) {
		File[] dirs = f.listFiles();
		this.setTitle("Current Dir: " + f.getName());
		List<Item> dir = new ArrayList<>();
		List<Item> fls = new ArrayList<>();
		for (File ff : dirs) {
			Date lastModDate = new Date(ff.lastModified());
			DateFormat formater = DateFormat.getDateTimeInstance();
			String date_modify = formater.format(lastModDate);
			if (ff.isDirectory()) {
				File[] fbuf = ff.listFiles();
				int buf = 0;
				if (fbuf != null) {
					buf = fbuf.length;
				} else buf = 0;
				String num_item = String.valueOf(buf);
				if (buf == 0) num_item = num_item + " item";
				else num_item = num_item + " items";
				dir.add(new Item(ff.getName(), num_item, date_modify, ff.getAbsolutePath(), "directory_icon"));
			} else {
				fls.add(new Item(ff.getName(), ff.length() + " Byte", date_modify, ff.getAbsolutePath(), "file_icon"));
			}
		}
		Collections.sort(dir);
		Collections.sort(fls);
		dir.addAll(fls);
		if (!f.getName().equalsIgnoreCase("sdcard"))
			dir.add(0, new Item("..", "Parent Directory", "", f.getParent(), "directory_up"));
		adapter = new FileArrayAdapter(FileChooserActivity.this, R.layout.fileview, dir);
		this.setListAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Item o = adapter.getItem(position);
		if (o.getImage().equalsIgnoreCase("directory_icon") || o.getImage().equalsIgnoreCase("directory_up")) {
			currentDir = new File(o.getPath());
			fill(currentDir);
		} else {
			onFileClick(o);
		}
	}

	private void onFileClick(Item o) {
		//Toast.makeText(this, "Folder Clicked: "+ currentDir, Toast.LENGTH_SHORT).show();
		Intent intent = new Intent();
		intent.putExtra("GetPath", currentDir.toString());
		intent.putExtra("GetFileName", o.getName());
		setResult(RESULT_OK, intent);
		BufferedReader input = null;
		try {
			input = new BufferedReader(new FileReader("/sdcard/" + o.getName()));
			StringBuilder contents = new StringBuilder();

			//use buffering, reading one line at a time
			//FileReader always assumes default encoding is OK!
			try {
				String line = null; //not declared within while loop

				while ((line = input.readLine()) != null) {
					contents.append(line);
					contents.append(System.getProperty("line.separator"));
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} catch (FileNotFoundException e) {
			System.out.println(e.getStackTrace());
			e.printStackTrace();
		}

		finish();
	}
}
